<template>
  <div style="text-align: center;border-radius: 8px;width: 350px;height: 350px;border: 2px solid var(--el-border-color);">
    <div style="font-size: 25px;margin-top: 30px;font-family: STXINWEI">登录</div>
    <div style="margin-top: 20px;margin-left: 10px;margin-right: 10px">
      <el-input v-model="form.username" type="text" placeholder="用户名/邮箱">
        <template #prefix>
          <el-icon><User /></el-icon>
        </template>
      </el-input>
      <el-input v-model="form.password" type="password" placeholder="密码" style="margin-top: 10px">
        <template #prefix>
          <el-icon><Lock /></el-icon>
        </template>
      </el-input>
      <el-row style="margin-top: 10px">
        <el-col :span="12" style="text-align:left">
          <el-checkbox v-model="form.remember" label="记住我"/>
        </el-col>
        <el-col :span="12" style="text-align:right">
          <el-link @click="router.push('/reset')">忘记密码？</el-link>
        </el-col>
      </el-row>
      <div style="margin: 20px">
        <el-button @click="login()" style="width: 250px" type="success" plain>登录</el-button>
      </div>
      <div style="margin: 20px">
        <el-button @click="router.push('/register')" style="width: 250px" type="warning" plain>注册</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ElMessage} from "element-plus";
import {reactive} from "vue";
import {post,get} from "@/utils";
import router from "@/router";
import {useStore} from "../../stores";

const form = reactive({
  username: '',
  password: '',
  remember: false
})
const store = useStore()

const login = () =>{
  if (!form.username || !form.password){
    ElMessage.warning('请填写用户名和密码！')
  }else {
    post('/api/auth/login',{
      username: form.username,
      password: form.password,
      remember: form.remember
    },(message) =>{
      ElMessage.success(message)
      get('api/user/me',(message) => {
        store.auth.user = message
        router.push('/index')
      },() => {
        store.auth.user =null
      })
    })
  }
}
</script>

<style scoped>

</style>